package com.datastructure.test.plusonetoarray;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;

public class PlusOneToArray {

    public static void main(String[] args) {
        PlusOneToArray plusOneToArray = new PlusOneToArray();
        int[] nums = {1,2,3};
        int[] ints = plusOneToArray.plusOne(nums);
        System.out.println(ints);
    }

    public int[] plusOne (int[] nums) {
        // write code here
        ArrayList<Integer> list = new ArrayList<>(nums.length+1);
        int carry = 0;
        for (int i = nums.length - 1; i >= 0; i--) {
            if(i== nums.length-1){
               carry = (nums[i]+1)/10;
               list.add((nums[i]+1)%10);
            } else {
                if(carry==1){
                    list.add((nums[i]+carry)%10);
                    carry = (nums[i]+carry)/10;
                } else {
                    list.add(nums[i]);
                }
            }
        }
        if(carry==1){
            list.add(carry);
        }
        int[] result = new int[list.size()];
        for (int i = list.size()-1; i >= 0; i--) {
            result[list.size()-1-i]= list.get(i);
        }
        return result;
    }
}
